const addres = require('../service/addres')

// 获取收货地址
const getAddres = async(ctx)=>{
    let res = await addres.getAddress(ctx.query)
    if(res.length > 0) {
        return ctx.body = {
            code:20000,
            data:res,
            total:res.length,
            msg:'获取成功'
        }
    }else{
        return ctx.body ={
            code:40001,
            msg:'获取失败'
        }
    }
}

// 新增收获地址
const AddAddres = async(ctx)=>{
    let res = await addres.addAddress(ctx.request.body)
    if(res){
        return ctx.body = {
            code:20000,
            msg:'添加成功'
        }
    }else{
        return ctx.body ={
            code:40001,
            msg:'添加失败'
        }
    }
}

// 修改收货地址
const updateAddres = async(ctx)=>{
    let res = await addres.updateAddress(ctx.request.body)
    if(res){
        return ctx.body = {
            code:20000,
            msg:'修改成功'
        }
    }else{
        return ctx.body ={
            code:40001,
            msg:'修改失败'
        }
    }
}

// 删除收货地址
const AddresDelete = async(ctx)=>{
    let res = await addres.deleteAddress(ctx.query)
    if(res){
        return ctx.body = {
            code:20000,
            msg:'删除成功'
        }
    }else{
        return ctx.body ={
            code:40001,
            msg:'删除失败'
        }
    }
}

// 设置默认地址
const setDefaultAddress = async (ctx)=>{
    let res = await addres.setDefaultAddress(ctx.request.body)
    if(res){
        return ctx.body = {
            code:20000,
            msg:'设置成功'
        }
    }else{
        return ctx.body ={
            code:40001,
            msg:'设置失败'
        }
    }
}

module.exports = {
    AddAddres,
    getAddres,
    updateAddres,
    AddresDelete,
    setDefaultAddress
}